<template>
  <div>
    <bread second="权限管理" third="角色列表"></bread>
    <el-card class="box-card" style="width:100%;margin-top:15px">
      <el-button type="primary">添加角色</el-button>
      <el-table :data="tableData" border style="width: 100%;">
        <el-table-column type="expand" label>
          <template slot-scope="scoped">
            <div v-for="(item1, index1) in scoped.row.children" :key="index1">
              <el-row>
                <el-col :span="8">
                  <el-tag closable type="success">{{ item1.authName }}</el-tag>
                  <i class="el-icon-caret-right"></i>
                </el-col>
                <el-col :span="14">
                  <el-row
                    v-for="(item2, index2) in item1.children"
                    :key="index2"
                  >
                    <el-col :span="8">
                      <el-tag closable type="success">{{
                        item2.authName
                      }}</el-tag>
                      <i class="el-icon-caret-right"></i>
                    </el-col>
                    <el-col :span="14">
                      <el-tag
                        v-for="(item3, index3) in item2.children"
                        :key="index3"
                        closable
                        type="success"
                        >{{ item3.authName }}</el-tag
                      >
                    </el-col>
                  </el-row>
                </el-col>
              </el-row>
            </div>
          </template>
        </el-table-column>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column
          prop="roleName"
          label="角色名称"
          width="180"
        ></el-table-column>
        <el-table-column
          prop="roleDesc"
          label="角色描述"
          width="180"
        ></el-table-column>
        <el-table-column prop="address" label="操作">
          <template slot-scope="scoped">
            <el-button
              type="primary"
              icon="el-icon-setting"
              @click="treeList(scoped.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 分配权限 -->
    <el-dialog title="分配权限" :visible.sync="dialogFormVisible">
      <el-tree
        :data="data"
        show-checkbox
        node-key="id"
        :default-expanded-keys="[1, 2, 3]"
        :default-checked-keys="defaultKey"
        :default-expand-all="true"
        :props="defaultProps"
        ref="tree"
      ></el-tree>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="setRight">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  getRightList_api,
  getRoleList_api,
  setRight_api
} from "@/api/index.js";
export default {
  data() {
    return {
      tableData: [], //表格数据
      //   rightAll: [], //所有权限数据
      dialogFormVisible: false,
      data: [], //树形控件数据
      defaultKey: [], //显示默认选中权限
      CheckedKeys: [],
      HalfCheckedKeys: [],
      defaultProps: {
        children: "children",
        label: "authName"
      },
      roleId: ""
    };
  },
  created() {
    // this.getRightList();
    this.getRight();
  },
  methods: {
    //获取所有权限
    async getRight() {
      const { data: res } = await getRoleList_api();
      // console.log(res, 3);
      this.tableData = res.data;
    },
    digui(row, arr) {
      if (!row.children) {
        return arr.push(row.id);
      } else {
        row.children.forEach(item => {
          this.digui(item, arr);
        });
      }
    },
    //获取权限列表
    async treeList(row) {
      this.dialogFormVisible = true;
      const { data: res } = await getRightList_api({ type: "tree" });
      // console.log(row);
      this.data = res.data;
      this.roleId = row.id;
      this.digui(row, this.defaultKey);
    },
    //分配权限
    async setRight() {
      let rids = [
        ...this.$refs.tree.getCheckedKeys(),
        this.$refs.tree.getHalfCheckedKeys()
      ].join(",");
      const { data: res } = await setRight_api({
        roleId: this.roleId,
        rids: rids
      });
      console.log(res, rids);
      if (res.meta.status == 200) {
        this.$message({
          message: res.meta.msg,
          type: "success"
        });
        this.dialogFormVisible = false;
        this.getRight();
      }
    }
  }
};
</script>
<style lang="less" scoped></style>
